package com.winit.openapi.gfs.controller;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.gfs.spi.EbayDeliveryWayConfigService;
import com.winit.gfs.spi.command.EbayDeliveryWayConfigCommand;
import com.winit.gfs.spi.command.QueryDeliveryWayConfigCommand;
import com.winit.gfs.spi.vo.EbayDeliveryWayConfigVO;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;

/**
 * 库存同步-下单方式配置
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * jianhua.huang 	1.0  		2015-8-10 	Created
 * 
 * </pre>
 * @since 1.
 */

@Controller
@RequestMapping(value = "/gfs/ebayDeliveryWay")
public class EbayDeliveryWayConfigController extends BaseController {

	@Resource
	private EbayDeliveryWayConfigService ebayDeliveryWayConfigService;

	/**
	 * 查询下单配置
	 * @param requestMsg
	 * @param responseMsg
	 * @return
	 */
	@RequestMapping(value = "/findConfig")
	public String findConfig(@ModelAttribute RequestMsg requestMsg,
			@ModelAttribute ResponseMsg responseMsg) {
	    Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            QueryDeliveryWayConfigCommand command = new QueryDeliveryWayConfigCommand();
            EbayDeliveryWayConfigVO wayConfigVO = null;
            
            CommandContext ctx = CommandContext.getContext();
            //获取当前买家ID
            command.setCtx(ctx);
            command.setBpartnerId(Long.parseLong(ctx.getUserId()));
            if(json.containsKey("platform")){
                command.setPlatform(json.getString("platform"));
            }else{
                command.setPlatform("ebay");
            }
            wayConfigVO = ebayDeliveryWayConfigService.findConfig(command);
            responseMsg.setData(buildReturnList(wayConfigVO));
        }else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
		return SUCCESS;
	}
	
	/**
	 * 保存或更新下单方式配置
	 * @param requestMsg
	 * @param responseMsg
	 * @return
	 */
	@RequestMapping(value = "/saveOrUpdateConfig")
	public String saveOrUpdateConfig(@ModelAttribute RequestMsg requestMsg,
			@ModelAttribute ResponseMsg responseMsg) {
		Object obj = requestMsg.getData();
		EbayDeliveryWayConfigCommand command=new EbayDeliveryWayConfigCommand();
		if (obj instanceof JSONObject) {
			JSONObject json = (JSONObject) obj;
			//构建数据
			EbayDeliveryWayConfigVO wayConfigVO = builSaveDate(json);
			CommandContext ctx = CommandContext.getContext();
			wayConfigVO.setcBpartnerId(Long.parseLong(ctx.getUserId()));
		    command.setCtx(ctx);
			command.setWayConfigVO(wayConfigVO);
			int result=ebayDeliveryWayConfigService.saveOrUpdateConfig(command);
			if(result<0){
				throw new ApiException(ErrorCode.SYSTEM_ERROR);
			}
		}
		else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
		return SUCCESS;
	}

	private EbayDeliveryWayConfigVO builSaveDate(JSONObject json){
		EbayDeliveryWayConfigVO configVO=new EbayDeliveryWayConfigVO();
		if(json.containsKey("ebayDeliveryWay")){
			configVO.setEbayDeliveryway(json.getString("ebayDeliveryWay"));
		}
		if(json.containsKey("pullOrder")){
		    configVO.setPullOrder(json.getString("pullOrder"));
		}
		if(json.containsKey("updateEbayInventory")){
		    configVO.setUpdateEbayInventory(json.getString("updateEbayInventory"));
		}
		if(json.containsKey("options")){
			configVO.setOptions(json.getString("options"));
		}
		if(json.containsKey("isOpenReturn")){
            configVO.setIsOpenReturn(json.getString("isOpenReturn"));
        }
		if(json.containsKey("ispLimitCost")){
			configVO.setIspLimitCost(json.getBigDecimal("ispLimitCost"));
		}
		if(json.containsKey("outboundLimitCost")){
			configVO.setOutboundLimitCost(json.getBigDecimal("outboundLimitCost"));
		}
		if(json.containsKey("addressCheck")){
			configVO.setAddressCheck(json.getString("addressCheck"));
		}
		if(json.containsKey("egdCheck")){
		    configVO.setEgdCheck(json.getString("egdCheck"));
		}
        if(json.containsKey("platform")){
            configVO.setPlatform(json.getString("platform"));
        }else{
            configVO.setPlatform("ebay");
        }
		return configVO;
	}
	
	/**
	 * 构建返回数据
	 * @param wayConfigVO
	 * @return
	 */
	private Map<String, Object> buildReturnList(
			EbayDeliveryWayConfigVO wayConfigVO) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(wayConfigVO==null){
			return map;
		}
		map.put("ebayDeliveryway", wayConfigVO.getEbayDeliveryway());
		map.put("ebayDeliverywayID", wayConfigVO.getEbayDeliverywayId());
		map.put("options", wayConfigVO.getOptions());
		map.put("isOpenReturn", wayConfigVO.getIsOpenReturn());
		map.put("updateEbayInventory", wayConfigVO.getUpdateEbayInventory());
		map.put("pullOrder", wayConfigVO.getPullOrder());
		map.put("ispLimitCost", wayConfigVO.getIspLimitCost());
		map.put("outboundLimitCost", wayConfigVO.getOutboundLimitCost());
		map.put("addressCheck", wayConfigVO.getAddressCheck());
		map.put("egdCheck", wayConfigVO.getEgdCheck());
		map.put("platform", wayConfigVO.getPlatform());
		return map;
	}
}
